# openpose多进程测试成功
import time
import cv2
import multiprocessing as mp
from src.utils.OpenposeProcess import openpose_process

if __name__ == '__main__':
    # Initialize the input and output queues for interprocess communication
    input_queue = mp.Queue()
    output_queue = mp.Queue()

    # Start the OpenPose process as a separate process
    openpose_proc = mp.Process(target=openpose_process, args=(input_queue, output_queue))
    openpose_proc.start()

    # Read the input video
    cap = cv2.VideoCapture("../assets/1.mp4")

    while True:
        start_time = time.time()

        # Read a frame from the video
        ret, frame = cap.read()

        if not ret:
            break

        # Put the frame into the input queue for the OpenPose process to recognize
        input_queue.put(frame)

        # Get the results from the output queue returned by the OpenPose process
        results = output_queue.get()

        # Draw the results on the frame
        if results is not None:
            frame = results["output_data"]

        # Display the frame
        cv2.imshow("Output", frame)
        end_time = time.time()
        seconds = end_time - start_time
        fps = round(1 / seconds)
        print(f'fps:{fps}')
        cv2.waitKey(1)

    # Signal the OpenPose process to stop by putting a None object into the input queue
    input_queue.put(None)

    # Wait for the OpenPose process to finish
    openpose_proc.join()

    # Release the video capture and close all windows
    cap.release()
    cv2.destroyAllWindows()
